//
//  CLBook.h
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/2/9.
//

#import "CLObject.h"
#import <LKDBHelper/LKDBHelper.h>
#import "CLReadConfigure.h"
NS_ASSUME_NONNULL_BEGIN
@class CLReadChapterModel,CLReadRecordModel,CLReadPageModel,CLReadMarkModel;

typedef NS_ENUM(NSInteger, CLPageHeadType) {
    CLPageHeadTypeChapterName,
    CLPageHeadTypeParagraph,
    CLPageHeadTypeLine
};

static NSUInteger CL_READ_RECORD_CURRENT_CHAPTER_LOCATION = 0;

@protocol CLNovelViewDelegate <NSObject>
- (void)cl_novelViewOpenChapter:(CLReadChapterModel *_Nullable)chapter indexPath:(NSIndexPath *_Nullable)indexPath;
@end

@interface CLReadChapterModel : CLObject
/// domain+author+title+name  <domain>title(author)-name
@property (nonatomic, copy) NSString *cid;
@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) NSString *author;
@property (nonatomic, copy) NSString *domain;
@property (nonatomic, copy) NSString *title;
//临时数据,过滤释放时候的日志
@property (nonatomic, assign) BOOL isTemp;
@property (nonatomic, copy) NSString *bookid;

@property (nonatomic, copy) NSString *chapterid;
/// 上一章ID
@property (nonatomic, copy, nullable) NSString *previousChapterID;
/// 下一章ID
@property (nonatomic, copy, nullable) NSString *nextChapterID;
/// 内容
/// 此处 content 是经过排版好且双空格开头的内容。
/// 如果是网络数据需要确认是否处理好了,也就是在网络章节数据拿到之后, 使用排版接口进行排版并在开头加上双空格。(例如: CL_READ_PH_SPACE + 排版好的content )
/// 排版内容搜索 contentTypesetting 方法
@property (nonatomic, copy) NSString *content;
/// 优先级 (一般章节段落都带有排序的优先级 从0开始)
@property (nonatomic, assign) NSInteger priority;
/// 本章有多少页
@property (nonatomic, assign) NSInteger pageCount;
/// 分页数据
@property (nonatomic, strong) NSArray<CLReadPageModel *> *pageModels;

// MARK: 快捷获取
/// 当前章节是否为第一个章节
@property (nonatomic, assign) BOOL isFirstChapter;
/// 当前章节是否为最后一个章节
@property (nonatomic, assign) BOOL isLastChapter;
/// 完整章节名称
@property (nonatomic, copy) NSString *fullName;
/// 完整富文本内容
@property (nonatomic, strong) NSAttributedString *fullContent;
/// 分页总高 (上下滚动模式使用)
@property (nonatomic, assign) CGFloat pageTotalHeight;
/// 章节名称
@property (nonatomic, copy) NSString *name;
- (void)requestWithCompleteBlock:(void(^)(void))completeBlock;
- (BOOL)isValid;
- (void)removeObserverAndBlock;
/// 是否存在章节内容
+ (BOOL)isExist:(NSString *)bookid chapterid:(NSString *)chapterid;
/// 获取章节对象,如果则创建对象返回
+ (CLReadChapterModel *)model:(NSString *)bookid chapterid:(NSString *)chapterid;
+ (CLReadChapterModel *)model:(NSString *)bookid chapterid:(NSString *)chapterid isUpdateFont:(BOOL)isUpdateFont;
/// 保存
- (void)save;
/// 更新字体
- (void)updateFont;
@end

@interface CLBook : CLObject
@property (nonatomic, copy) NSString *bookid;///domain+author+title   <domain>title(author)
@property (nonatomic, copy) NSString *bid;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *logo;
@property (nonatomic, copy) NSString *intro;
@property (nonatomic, copy) NSString *author;
@property (nonatomic, strong) NSArray <CLReadChapterModel*>*catalogue;
@property (nonatomic, copy) NSString *url;
@property (nonatomic, copy) NSString *domain;
@property (nonatomic, assign) BOOL isHadCatalogue;//目录是否需要更新
@property (nonatomic, assign) BOOL isTemp;//临时数据
@property (nonatomic, strong) CLReadRecordModel *recordModel;/// 当前阅读记录
- (void)addObserverWithUpdateBlock:(void(^)(void))updateBlock;
- (BOOL)isValid;
+ (NSString *)bookidFormat;
@end

@interface CLReadPageModel : CLObject
// MARK: 常用属性
/// 当前页内容
@property (nonatomic, strong) NSAttributedString *content;
/// 当前页范围
@property (nonatomic, assign) NSRange range;
/// 当前页序号
@property (nonatomic, assign) NSInteger page;
// MARK: 滚动模式使用
/// 根据开头类型返回开头高度 (目前主要是滚动模式使用)
@property (nonatomic, assign) CGFloat headTypeHeight;
/// 当前内容Size (目前主要是(滚动模式 || 长按模式)使用)
@property (nonatomic, assign) CGSize contentSize;
/// 当前内容头部类型 (目前主要是滚动模式使用)
@property (nonatomic, assign) CLPageHeadType headType;
/// 当前内容总高(cell 高度)
- (CGFloat)cellHeight;
/// 书籍首页
@property (nonatomic, assign) BOOL isHomePage;
/// 获取显示内容(考虑可能会变换字体颜色的情况)
@property (nonatomic, strong) NSAttributedString *showContent;
@end

@interface CLReadRecordModel : CLObject
@property (nonatomic, copy) NSString *bookid;/// domain+author+title   <domain>title(author)
@property (nonatomic, strong) CLReadChapterModel *chapterModel;/// 当前记录的阅读章节
@property (nonatomic, assign) NSInteger page;/// 阅读到的页码(上传阅读记录到服务器时传当前页面的 location 上去,从服务器拿回来 location 在转成页码。精准回到上次阅读位置)
// MARK: 快捷获取
@property (nonatomic, strong) CLReadPageModel *pageModel;
/// 当前记录起始坐标
@property (nonatomic, assign) NSUInteger locationFirst;
/// 当前记录末尾坐标
@property (nonatomic, assign) NSUInteger locationLast;
/// 当前记录是否为第一个章节
@property (nonatomic, assign) BOOL isFirstChapter;
/// 当前记录是否为最后一个章节
@property (nonatomic, assign) BOOL isLastChapter;
/// 当前记录是否为第一页
@property (nonatomic, assign) BOOL isFirstPage;
/// 当前记录是否为最后一页
@property (nonatomic, assign) BOOL isLastPage;
/// 当前记录页码字符串
@property (nonatomic, copy) NSString *contentString;
/// 当前记录页码富文本
@property (nonatomic, strong) NSAttributedString *contentAttributedString;

/// 当前记录切到上一页
- (void)previousPage;

/// 当前记录切到下一页
- (void)nextPage;

/// 当前记录切到第一页
- (void)firstPage;

/// 当前记录切到最后一页
- (void)lastPage;

// MARK: 辅助

/// 修改阅读记录为指定章节位置
- (void)modify:(CLReadChapterModel *)chapterModel page:(NSInteger)page;
- (void)modify:(CLReadChapterModel *)chapterModel page:(NSInteger)page isSave:(BOOL)isSave;

/// 修改阅读记录为指定章节位置
- (void)modify:(NSString *)chapterid location:(NSInteger)location isSave:(BOOL)isSave;

/// 保存记录
- (void)save;

/// 修改阅读记录为指定章节页码 (toPage == DZM_READ_LAST_PAGE 为当前章节最后一页)
- (void)modify:(NSString *)chapterid toPage:(NSInteger)toPage isSave:(BOOL)isSave;

/// 更新字体
- (void)updateFont:(BOOL)isSave;

/// 拷贝阅读记录
- (CLReadRecordModel *)copyModel;

/// 是否存在阅读记录
+ (BOOL)isExist:(NSString *)bookid;


// MARK: 构造

/// 获取阅读记录对象,如果则创建对象返回
+ (CLReadRecordModel *)model:(NSString *)bookid;
@end


@interface CLReadMarkModel : CLObject
/// 小说ID
@property (nonatomic, copy) NSString *bookid;

/// 章节ID
@property (nonatomic, copy) NSString *chapterid;

/// 章节名称
@property (nonatomic, copy) NSString *name;

/// 内容
@property (nonatomic, copy) NSString *content;

/// 时间戳
@property (nonatomic, assign) NSTimeInterval time;

/// 位置
@property (nonatomic, assign) NSInteger location;
@end

NS_ASSUME_NONNULL_END
